# -*- coding: utf-8 -*-

from odoo import models, fields, api, exceptions


class SpeechRegistration(models.Model):
    _name = 'speech.registration'
    _description = '演讲报名'
    _order = 'create_date desc'
    _rec_name = 'topic'

    activity_id = fields.Many2one('speech.activity', string='活动', required=True, ondelete='cascade', index=True)
    user_id = fields.Many2one('wechat.user', string='用户', required=True, ondelete='cascade', index=True)
    
    name = fields.Char(string='姓名', required=True)
    phone = fields.Char(string='手机号', required=True)
    email = fields.Char(string='邮箱')
    
    topic = fields.Char(string='演讲主题', required=True, index=True)
    introduction = fields.Text(string='演讲简介', required=True)
    remark = fields.Text(string='备注')
    
    attachment_ids = fields.One2many('speech.registration.attachment', 'registration_id', string='附件')
    
    status = fields.Selection([
        ('pending', '待审核'),
        ('approved', '已通过'),
        ('rejected', '已驳回')
    ], string='状态', default='approved', required=True, index=True)
    
    feedback = fields.Text(string='审核反馈')
    reviewed_by = fields.Many2one('res.users', string='审核人')
    reviewed_date = fields.Datetime(string='审核时间')
    
    vote_ids = fields.One2many('speech.vote.line', 'candidate_id', string='获得的加油')
    vote_count = fields.Integer(string='加油数', compute='_compute_vote_count', store=True)
    
    _sql_constraints = [
        ('user_activity_unique', 'UNIQUE(user_id, activity_id)', '您已报名该活动！')
    ]

    @api.depends('vote_ids')
    def _compute_vote_count(self):
        for record in self:
            record.vote_count = len(record.vote_ids)

    def action_approve(self):
        """通过审核"""
        self.write({
            'status': 'approved',
            'reviewed_by': self.env.user.id,
            'reviewed_date': fields.Datetime.now()
        })

    def action_reject(self, feedback=''):
        """驳回审核"""
        self.write({
            'status': 'rejected',
            'feedback': feedback,
            'reviewed_by': self.env.user.id,
            'reviewed_date': fields.Datetime.now()
        })

    @api.model
    def create(self, vals):
        # 检查报名时间
        activity = self.env['speech.activity'].browse(vals.get('activity_id'))
        now = fields.Datetime.now()
        
        if now < activity.registration_start_time:
            raise exceptions.ValidationError('报名尚未开始！')
        
        if now > activity.registration_end_time:
            raise exceptions.ValidationError('报名已结束！')
        
        return super(SpeechRegistration, self).create(vals)


class SpeechRegistrationAttachment(models.Model):
    _name = 'speech.registration.attachment'
    _description = '报名附件'
    _rec_name = 'filename'

    registration_id = fields.Many2one('speech.registration', string='报名', required=True, ondelete='cascade')
    filename = fields.Char(string='文件名', required=True)
    file_url = fields.Char(string='文件URL', required=True)
    file_size = fields.Integer(string='文件大小')
    file_type = fields.Char(string='文件类型')

